
import Common from '../../../public/actions/employing/common';
import {getAppPageConfig} from '../../../../hrpub/common/utils/utils';
export default class ButtonAction extends Common {
    constructor(comp) {
        super();
        this.action = comp.action;
        this.comp = comp;
    }
    appConfig = getAppPageConfig();
    didAllInstance = () => {
        this.setBtnHandleMap();
    }

    // 生成按钮map
    setBtnHandleMap = () => {
        this.buttonHandleMap = {
            // 查询
            'query': this.showSearchModal,
            // 刷新
            'refresh': this.refresh,
            // 新增
            'add': this.action.formAct.toAddPage,
            // 删除
            'delete': this.action.rowAct.deleteRows,
            // 提交
            'submit': this.action.rowAct.submitOnes,
            // 收回
            'callback': this.action.rowAct.callbackOnes,
            // 执行
            'excute': this.action.rowAct.excuteOnes,
            // 编制情况
            'CheckBudget': this.action.orgSta.openOrgSituationModal,
            // 批量新增
            'batchadd': this.action.batAddAct.openBatchModal,
            // 打印
            'print': this.printTable,
            // 输出
            'output': this.outputTable,
            // 联查人员卡片
            'cardrpt': this.previewCard
        }
    }

    // 联查人员卡片
    previewCard = () => {
        let rows = this.comp.props.editTable.getCheckedRows('list');
        this.wpsview.openView(rows)
    }

    // 刷新
    refresh = () => {
        this.pubSub.publish('getMainTableData');
    }

    // 第一次进页面的按钮状态
    initPageButton = () => {
        const {props} = this.comp;
        const {
            button
        } = props;

        button.setButtonVisible({
            save: false,
            cancel: false,
            first: false,
            pre: false,
            next: false,
            last: false,
            copy: false,
            assfunc: true,
            approveinfo: false,
            edit: false,
            cardrpt: false,
            file: false,
            send_offer: false,
            accept_offer: false,
            reject_offer: false,
            submit_offer: false
        });
        button.setButtonDisabled({
            add: true,
            edit: true,
            delete: true,
            copy: true,
            query: true,
            submit: true,
            assfunc: true,
            print: true,
            batchadd: true,
            callback: true,
            approveinfo: true,
            refresh: true,
            excute: true
        });
    }

    // 按钮点击回调
    headerClick = (props, btnCode) => {
        console.log(btnCode);
        if(typeof this.buttonHandleMap[btnCode] === 'function') {
            this.buttonHandleMap[btnCode]();
        }
    }

    // 展示查询弹窗
    showSearchModal = () => {
        const {props} = this.comp;
        const {search} = props;

        search.openAdvSearch('querybill', true);
    }

    // 人力组织改变
    changeOrg = (value) => {
        const {props} = this.comp;
        const {
            button,
            emp
        } = props;
        if(typeof value == 'string'){
            this.update({
                orgValue: null,
                widget:true
            });
            this.pubSub.publish('getWidgetTableData', {
                value: value
            });
        }else{
            if(value) {
                button.setButtonVisible({
                    add: true,
                    delete: true,
                    query: true,
                    submit: true,
                    assfunc: true,
                    print: true,
                    batchadd: true,
                    callback: true,
                    refresh: true,
                    cardrpt: true
                });
                button.setButtonDisabled({
                    add: false,
                    edit: false,
                    delete: false,
                    copy: false,
                    query: false,
                    submit: false,
                    assfunc: false,
                    print: false,
                    batchadd: false,
                    callback: false,
                    approveinfo: false,
                    refresh: false,
                    excute: false    
                });
            }
            this.update({
                orgValue: Object.keys(value).length <= 0 ? null :value,
                widget:false
            });
            // 清空的情况
            if(Object.keys(value).length <= 0 && emp.orgValue && Object.keys(emp.orgValue).length > 0) {
                this.pubSub.publish('getMainTableData', {
                    pk_org: 'empty'
                });
            }
            // let refpk = value.refpk || '';
            // this.isApprFlow(refpk)
        }
    }

    //  // 获取是工作流还是审批流
    //  isApprFlow = async(pk_org) => {
    //     const {props} = this.comp;
    //     const {dispatch, emp} = props;
    //     console.log(pk_org)
    //     let postData = {
    //         appCode: this.appConfig? this.appConfig.appcode : '60092010',
    //         pk_org: pk_org
    //     }
    //     let res = await dispatch({
    //         type: 'emp/checkIsApprFlow',
    //         payload:  {
    //             postData: postData
    //         }
    //     })
    //     if (res.success) {
    //         this.update({isApprFlow: res.data})
    //     }
    // }

    // 打印
    printTable = () => {
        const {props} = this.comp;
        const {emp, editTable, meta} = props;

        let tableWrapper = document.getElementById('mainTable');

        this.print(tableWrapper, {
            title: emp.language['gx6008-000067'], // 录用办理
            maker: emp.language['gx6008-000068'], // 制作者
            date: emp.language['gx6008-000069'], // 制作日期
            beforeAppend: (data) => {
                data[0].map((item, rowIndex) => {
                    delete item[0];
                });
                data[1].map((item) => {
                    item.length = item.length - 1;
                });
                return data;
            }
        }, {
            data: editTable.getAllRows('list'),
            tableTmp: meta.getMeta()['list']
        });
    }

    // 输出
    outputTable = () => {
        const {
            props: {
                editTable,
                emp: {
                    language
                },
                meta
            }
        } = this.comp;
        let tableWrapper = document.getElementById('mainTable');
        let tableData = editTable.getAllData('list');

        this.exportHtml(tableWrapper, {
            fileName: language['gx6008-000067'] // '录用办理'
        }, {
            meta: meta.getMeta()['list'],
            data: tableData.rows,
            showIndex: false
        });
    }

    // 附件管理
    fileManager = (selectedRows) => {
        let billId = selectedRows[0].values['pk_entryapply'].value;

        this.update({
            fileManagerBillId: billId,
            fileManagerModalVisible: true
        });
    }
}